package com.blb.eduadservice.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import java.net.HttpURLConnection;
import java.net.URL;
import java.time.LocalDateTime;

/**
 * 任务调度配置
 */
@Slf4j
@EnableScheduling
@Configuration
public class ScheduleConfig {

    /**
     * 更新广告数据任务
     */
    @Scheduled(cron = "0/5 * * * * ? ")
    public void updateAdTask(){
        try {
            updateAd();
        } catch (Exception e) {
            log.error("出现异常",e);
        }
    }

    private void updateAd() throws Exception {
        URL url = new URL("http://localhost:7777/ad_update?space_id=1");
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setRequestMethod("GET");
        conn.setConnectTimeout(2000);
        if(conn.getResponseCode() == 200){
            log.info("更新成功! {}",LocalDateTime.now());
        }else{
            log.error("更新失败! {}",LocalDateTime.now());
        }
        conn.disconnect();
    }
}
